package com.library.imageeditor.demo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xh.imageeditor.sdk.IMGEditActivity;
import com.xh.imageeditor.sdk.core.util.FileChooseUtils;

import java.io.File;
import java.util.UUID;


public class DemoActivity extends AppCompatActivity {

    private TextView mResultTv;
    private ImageView mSrcIv;
    private ImageView mEditIv;
    private Button mSelectBtn;
    private Button mEditBtn;

    private Uri fullPhotoUri;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        mResultTv = findViewById(R.id.tv_result);
        mSrcIv = findViewById(R.id.iv_src);
        mEditIv = findViewById(R.id.iv_edit);
        mSelectBtn = findViewById(R.id.btn_select);
        mEditBtn = findViewById(R.id.btn_edit);

        mSelectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mResultTv.setText("");
                mEditIv.setImageURI(null);

                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.setType("image/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 100);
            }
        });
        mEditBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fullPhotoUri == null) {
                    Toast.makeText(DemoActivity.this, "请先选择图片", Toast.LENGTH_LONG).show();
                    return;
                }
                mResultTv.setText("");
                mEditIv.setImageURI(null);

                IMGEditActivity.startForResult(DemoActivity.this, 101, fullPhotoUri);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    fullPhotoUri = data.getData();
                    mSrcIv.setImageURI(fullPhotoUri);
                    break;
                case 101:
                    String imagePath = data.getStringExtra(IMGEditActivity.RESULT_IMAGE_PATH);
                    mResultTv.setText(imagePath);
                    mEditIv.setImageURI(Uri.fromFile(new File(imagePath)));
                    break;
            }
        }
    }
}
